<?php

// +----------------------------------------------------------------------
// | 会员模型
// +----------------------------------------------------------------------

namespace Home\Model;

use Common\Model\Model;
use Think\Model\RelationModel;

class UserModel extends RelationModel {
    protected $_link = array(
        'UserAttention' => array(
            'mapping_type'  => self::HAS_MANY,
            'class_name'    =>  'UserAttention',
            'foreign_key'   =>  'userid',
            'as_fields'     =>  'myid',
        ),
    );

    /**
     * 分享达人排行
     * @param $num
     * @return mixed
     */
    public function sharedaren($num,$myuid){
        $where['status']=array('EQ',true);
        $data=$this->where($where)->field('userid,nickname,uavatar,worksnum,attentionnum,fansnum,openuid')->order('worksnum DESC,fansnum DESC')->limit($num)->select();
        foreach($data as $key=>$value){
            if(M('UserFans')->where(array('userid'=>$value['userid'],'myid'=>$myuid,'status'=>true))->count()>0){
                $data[$key]['isfans']=true;
            }
        }
        return $data;
    }


    /**
     * 返回赔友动态排行
     * @param $num
     * @param $order
     * @return mixed
     */
    public function userDynamic($num){
        $Model=new Model();
        $userDynamicData=$Model
            ->table(array(
                    C('DB_PREFIX').'user_dynamic'=>'D',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('D.userid=U.userid and D.status=true')
            ->field('D.* , U.uname,U.uavatar,U.openuid')
            ->order('D.create_time DESC,D.id DESC')
            ->limit($num)
            ->select();
        return $userDynamicData;
    }



}
